package com.epay;

import java.io.File;
import java.io.FileWriter;
import java.util.Map;

public class CreaterDao {

	public static boolean createrDao(Map<String,Map<String,String>> map){
		try{
			 String workpath = Constants.workpath;
			 String tableNameLower=Constants.fileName;
			 String tableName=tableNameLower.substring(0,1).toUpperCase()+tableNameLower.substring(1);
			 //创建dao文件
			 String dir = workpath + File.separator + CreaterUtil.replaceAll(Constants.dao_package, ".", File.separator);
		     File dirfile = new File(dir);
		     if (!dirfile.exists()) {
		       dirfile.mkdirs();
		     }
		     
		     File beanfile = new File(dir+"/" + tableName+"Dao.java");
		     FileWriter fw2 = new FileWriter(beanfile);
		     fw2.write("package " + Constants.dao_package + ";\n");
		     fw2.write("import "+Constants.searchModel_package+"."+tableName+"DO;\n");
		     fw2.write("import com.epay.common.base.BaseDao;\n");
		     fw2.write("import org.apache.ibatis.annotations.Mapper;\n");
			/* fw2.write("import org.apache.ibatis.annotations.Select;\n");
			fw2.write("import org.apache.ibatis.annotations.Update;\n");
			fw2.write("import org.apache.ibatis.annotations.Delete;\n");
			fw2.write("import org.apache.ibatis.annotations.Insert;\n");
			 fw2.write("import java.util.List;\n");*/


		     fw2.write("\n");
		     fw2.write("/**\n");
			 fw2.write(" * @author Epay\n");
			 fw2.write(" * @date "+Constants.getSysDate()+"\n");
		     fw2.write(" */\n");
		     fw2.write("@Mapper\n");
		     fw2.write("public interface " + tableName + "Dao extends BaseDao<"+tableName+"DO> {");
		     fw2.write("\n");




			//查询语句
			/*fw2.write("\t@Select(\"<script>\"\n");
			fw2.write("\t\t +\"SELECT  *  FROM "+Constants.tableName.toUpperCase()+" <where> \"\n");
			*//*for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				Map.Entry e = (Map.Entry) it.next();
				String dbName=String.valueOf(e.getKey());
				Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
				String name=pamentMap.get("name");
				String dataType=pamentMap.get("dataType");
				dataType= CreaterUtil.getJavaForDbType(dataType.toUpperCase());
				if(dataType.equals("BIGINT")||dataType.equals("INTEGER")){
					fw2.write("\t\t+\"<if test=\\\""+name+" != null \\\">");
				}else{
					fw2.write("\t\t+\"<if test=\\\""+name+" != null and "+name+" != ''\\\">");
				}

				fw2.write("AND "+dbName+"=#{"+name+",jdbcType="+dataType+"} ");
				fw2.write("</if>\"\n");
			}*//*
			fw2.write("\t+ \"</where></script>\")\n");
			fw2.write("\tpublic List<"+tableName+"> findList("+tableName+" "+tableNameLower+");\n");

			fw2.write("\n\n");
			//根据id查询语句
			fw2.write("\t@Select(\"<script>\"\n");
			fw2.write("\t\t +\"SELECT  *  FROM "+Constants.tableName.toUpperCase()+"\"\n");

			fw2.write("\t\t+ \"where id = #{id,jdbcType=INTEGER}\"\n");
			fw2.write("\t+ \"</script>\")\n");
			fw2.write("\tpublic "+tableName+" get("+tableName+" "+tableNameLower+");\n");


			fw2.write("\n\n");
			//修改语句
			fw2.write("\t@Update(\"<script>\"\n");
			fw2.write("\t\t +\"Update "+Constants.tableName.toUpperCase()+"  SET \"\n");
			int i=0;
			for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				i++;
				Map.Entry e = (Map.Entry) it.next();
				String dbName=String.valueOf(e.getKey());
				Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
				String name=pamentMap.get("name");
				String dataType=pamentMap.get("dataType");
				dataType= CreaterUtil.getJavaForDbType(dataType.toUpperCase());
				if(dbName.toUpperCase().equals("ID")){
					continue;
				}
				if(i==map.size()){
					fw2.write("\t\t+\""+dbName+"= #{"+name+",jdbcType="+dataType+"}\"\n");
				}else{
					fw2.write("\t\t+\""+dbName+"= #{"+name+",jdbcType="+dataType+"},\"\n");
				}
			}
			fw2.write("\t\t+ \"WHERE ID = #{id,jdbcType=INTEGER}\"\n");
			fw2.write("\t+ \"</script>\")\n");
			fw2.write("\tpublic Integer update("+tableName+" "+tableNameLower+");\n");
			fw2.write("\n\n");
			//新增语句
			fw2.write("\t@Insert(\"<script>\"\n");
			fw2.write("\t\t +\"INSERT INTO "+Constants.tableName.toUpperCase()+"  \"\n");
			String keys="";
			String values="";
			for (Iterator it = map.entrySet().iterator(); it.hasNext();) {

				Map.Entry e = (Map.Entry) it.next();
				String dbName=String.valueOf(e.getKey());
				Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
				String name=pamentMap.get("name");
				String dataType=pamentMap.get("dataType");
				dataType= CreaterUtil.getJavaForDbType(dataType.toUpperCase());
				if(dbName.toUpperCase().equals("ID")){
					continue;
				}
				keys=keys+dbName+",";
				values=values+"#{"+name+",jdbcType="+dataType+"},";

			}
			keys=keys.substring(0,keys.length()-1);
			values=values.substring(0,values.length()-1);
			fw2.write("\t\t+ \"("+keys+")\"\n");
			fw2.write("\t\t+ \"("+values+")\"\n");
			fw2.write("\t+ \"</script>\")\n");
			fw2.write("\tpublic Integer insert("+tableName+" "+tableNameLower+");\n");


			fw2.write("\n\n");
			//删除语句
			fw2.write("\t@Delete(\"<script>\"\n");
			fw2.write("\t\t +\"DELETE FROM "+Constants.tableName.toUpperCase()+" <where> \"\n");
			for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				Map.Entry e = (Map.Entry) it.next();
				String dbName=String.valueOf(e.getKey());
				Map<String,String> pamentMap=(HashMap<String,String>)e.getValue();
				String name=pamentMap.get("name");
				String dataType=pamentMap.get("dataType");
				dataType= CreaterUtil.getJavaForDbType(dataType.toUpperCase());
				if(dataType.equals("BIGINT")||dataType.equals("INTEGER")){
					fw2.write("\t\t+\"<if test=\\\""+name+" != null \\\">");
				}else{
					fw2.write("\t\t+\"<if test=\\\""+name+" != null and "+name+" != ''\\\">");
				}

				fw2.write("AND "+dbName+" = #{"+name+",jdbcType="+dataType+"} ");
				fw2.write("</if>\"\n");
			}
			fw2.write("\t\t+ \"</where>\"\n");
			fw2.write("\t+ \"</script>\")\n");
			fw2.write("\tpublic Integer delete("+tableName+" "+tableNameLower+");\n");
			fw2.write("\n\n");
			//删除语句
			fw2.write("\t@Delete(\"<script>\"\n");
			fw2.write("\t\t +\"DELETE FROM "+Constants.tableName.toUpperCase()+" \"\n");
			fw2.write("\t\t+ \"where id= #{id,jdbcType=INTEGER}\"\n");
			fw2.write("\t+ \"</script>\")\n");
			fw2.write("\tpublic Integer deleteByPrimaryKey("+tableName+" "+tableNameLower+");\n");
*/
			fw2.write("}\n");
			fw2.close();
			 
		}catch(Exception e){
			System.out.println("创建bean异常");
			e.printStackTrace();
		}
		return true;
	}
}
